package tongcheng.com.tongcheng.base;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yanzhenjie.nohttp.RequestMethod;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.adapter.QiangDanPeopleDetialAdapter;
import tongcheng.com.tongcheng.bean.OrderDetialBean;
import tongcheng.com.tongcheng.bean.QiangDanPeopleBean;
import tongcheng.com.tongcheng.bean.StatusBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GlideImageLoader;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.Setting;
import tongcheng.com.tongcheng.utils.ShowToast;
import tongcheng.com.tongcheng.widght.MyBanner;

public class ChooseOrderPeopleActivity extends BaseActivity {


    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.back_text)
    TextView mBackText;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.allorderdetial_banner)
    MyBanner mAllorderdetialBanner;
    @Bind(R.id.detial_cardno)
    TextView mDetialCardno;
    @Bind(R.id.detial_price)
    TextView mDetialPrice;
    @Bind(R.id.detial_dianpu_name)
    TextView mDetialDianpuName;
    @Bind(R.id.detial_dianpu_phone)
    TextView mDetialDianpuPhone;
    @Bind(R.id.detial_dianpu_area)
    TextView mDetialDianpuArea;
    @Bind(R.id.detial_shigong_leixing)
    TextView mDetialShigongLeixing;
    @Bind(R.id.detial_name)
    TextView mDetialName;
    @Bind(R.id.detial_phone)
    TextView mDetialPhone;
    @Bind(R.id.detial_starttime)
    TextView mDetialStarttime;
    @Bind(R.id.detial_endtime)
    TextView mDetialEndtime;
    @Bind(R.id.detial_shigong_renshu)
    TextView mDetialShigongRenshu;
    @Bind(R.id.detial_install_info)
    TextView mDetialInstallInfo;
    @Bind(R.id.detial_install_rc)
    RecyclerView mDetialInstallRc;
    @Bind(R.id.allorderdetial_left)
    Button mAllorderdetialLeft;
    @Bind(R.id.allbutton)
    LinearLayout mAllbutton;
    private int mId;
    private int mOrder_type;
    private int mStatus;
    private OrderDetialBean.DataBean mData;
    public List<QiangDanPeopleBean.DataBean> datas = new ArrayList<>();
    private QiangDanPeopleDetialAdapter mQiangDanPeopleDetialAdapter;

    @Override
    public Integer initResid() {
        return R.layout.activity_choose_order_people;
    }

    public int rel_id = -1;
    @Override
    public void initCreate(Bundle savedInstanceState) {
        settitlebar(mBg);
        if (getTintManager() != null) {
            getTintManager().setNavigationBarTintResource(R.color.title_bar);
        }

        //TODO 下面2个按钮的各种判断事件
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mDetialInstallRc.setLayoutManager(linearLayoutManager);
        mQiangDanPeopleDetialAdapter = new QiangDanPeopleDetialAdapter(this,datas);
        mDetialInstallRc.setAdapter(mQiangDanPeopleDetialAdapter);


        mTitle.setText("订单详情");
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        mAllorderdetialLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                for (int i = 0; i < datas.size(); i++) {
                    QiangDanPeopleBean.DataBean dataBean = datas.get(i);
                    if (dataBean.isCheck) {
                        rel_id = dataBean.rel_id;
                    }
                }

                if (mAllorderdetialLeft.getText().toString().equals("确认抢单方")) {
                    chooseOrderShigong();
                } else {
                    ShowToast.showToast("请耐心等待发单方确认");
                }

            }
        });

        //初始化订单数据
        initOrderData();


    }
    //选择订单施工方
    private void chooseOrderShigong() {

        MyRequest request = new MyRequest(ConStans.OrderDetialButton_Url, RequestMethod.POST);
        request.add("type", 0);
        request.add("id", mData.id);
        request.add("cartid", rel_id);
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("ssssss",json + "");
                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {

                    ShowToast.showToast(statusBean.msg+"");

                    if (statusBean.status == 1) {
                        finish();
                    }
                }
            }
        });
    }


    private void initOrderData() {
        mId = getIntent().getIntExtra("id", -1);
        mOrder_type = getIntent().getIntExtra("order_type", -1);
        mStatus = getIntent().getIntExtra("status", -1);
        Log.e("login_id",mId+"");
        Log.e("login_type",mOrder_type+"");
        Log.e("login_status",mStatus+"");
        MyRequest request = new MyRequest(ConStans.OrderDetial_Url);
        request.add("id", mId);
        request.add("userid", Setting.getId());
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("ssssss",json + "");
                OrderDetialBean orderDetialBean = GsonUtil.GsonToBean(json, OrderDetialBean.class);
                if (orderDetialBean != null) {
                    ShowToast.showToast(orderDetialBean.msg);

                    if (orderDetialBean.status == 1) {
                        if (orderDetialBean.data != null) {
                            mData = orderDetialBean.data;
                            initRecycleData();
                            BindData(orderDetialBean.data);
                        }
                    }
                }
            }
        });
    }
    //初始化确认抢单用户接口数据
    private void initRecycleData() {
        MyRequest request = new MyRequest(ConStans.QueRenPeople_Url);
        request.add("orderId", mId);
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("ssssss",json + "");
                QiangDanPeopleBean qiangDanPeopleBean = GsonUtil.GsonToBean(json, QiangDanPeopleBean.class);
                if (qiangDanPeopleBean != null) {
                    List<QiangDanPeopleBean.DataBean> data = qiangDanPeopleBean.data;
                    if (data != null) {
                        datas.clear();
                        datas.addAll(data);
                        mQiangDanPeopleDetialAdapter.notifyDataSetChanged();
                    }

                }
            }
        });
    }

    private void BindData(OrderDetialBean.DataBean data) {


        if (Setting.getId() == data.account_id) {
            mAllorderdetialLeft.setText("确认抢单方");
        } else {
            mAllorderdetialLeft.setText("待确认");
        }
        mDetialCardno.setText(data.order_no);//订单编号
        mDetialPrice.setText(data.price);//订单价格
        mDetialDianpuName.setText(data.shop_name);//店铺名称
        mDetialDianpuPhone.setText(data.shop_phone);//店铺电话
        mDetialDianpuArea.setText(data.shop_address);//店铺地址
        mDetialShigongLeixing.setText(data.order_title);//施工类型
        mDetialName.setText(data.principal);//负责人
        mDetialPhone.setText(data.phone);
        mDetialStarttime.setText(data.expect_start_time_format);//发布日期
        mDetialEndtime.setText(data.expect_end_time_format);//结束日期
        mDetialShigongRenshu.setText(data.work_num + "人");
        mDetialInstallInfo.setText(data.scene_comment);//安装信息




        List<String> path = new ArrayList<>();
        if (!TextUtils.isEmpty(data.scene_pic)) {
            String[] split = data.scene_pic.split(",");
            for (int i = 0; i < split.length; i++) {
                path.add(ConStans.BaseUrl_Test_url + split[i]);
            }
        }

        //设置完成之前的图片
        //设置banner样式
        //mAllorderdetialBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //设置图片加载器
        mAllorderdetialBanner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        mAllorderdetialBanner.setImages(path);
        //设置banner动画效果
        mAllorderdetialBanner.setBannerAnimation(Transformer.DepthPage);
      /*  //设置标题集合（当banner样式有显示title时）
        mAllorderdetialBanner.setBannerTitles(titles);*/
        //设置自动轮播，默认为true
        mAllorderdetialBanner.isAutoPlay(true);
        //设置轮播时间
        mAllorderdetialBanner.setDelayTime(1500);
        //设置指示器位置（当banner模式中有指示器时）
        mAllorderdetialBanner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        mAllorderdetialBanner.start();




    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
